package com.gxs.tga.poc.rest.common;

import javax.ws.rs.core.Response;
import javax.ws.rs.ext.ExceptionMapper;

import org.codehaus.jackson.map.exc.UnrecognizedPropertyException;

import com.gxs.tga.poc.domain.exceptions.InvalidClientRequest;

public class GenericExceptionMapper implements ExceptionMapper<Exception>
{

	@Override
	public Response toResponse(Exception exception)
	{
		exception.printStackTrace();
		
		if(exception instanceof UnrecognizedPropertyException)
		{
			int status = Response.Status.BAD_REQUEST.getStatusCode();
	        TgaRestResponse response = new TgaRestResponse(status,"Exception occurred: " + exception.getMessage(), status );
	        return Response.status(status).entity(response).type("application/json").build();

		}
		else if(exception instanceof InvalidClientRequest )
		{
			int status = Response.Status.BAD_REQUEST.getStatusCode();
	        TgaRestResponse response = new TgaRestResponse(status,"Exception occurred: " + exception.getMessage(), status );
	        return Response.status(status).entity(response).type("application/json").build();

		}
		else if(exception instanceof com.sun.jersey.api.NotFoundException)
		{
			int status = Response.Status.BAD_REQUEST.getStatusCode();
	        TgaRestResponse response = new TgaRestResponse(status,"Unknown Request Api: " + exception.getMessage() );
	        return Response.status(status).entity(response).type("application/json").build();

		}
		else if(exception instanceof javax.ws.rs.WebApplicationException)
		{
			int status = Response.Status.BAD_REQUEST.getStatusCode();
	        TgaRestResponse response = new TgaRestResponse(status,"This combination of URL and Method and Parameters is not supported: " + exception.getMessage() );
	        return Response.status(status).entity(response).type("application/json").build();
			
		}
		else
		{
	        int status = Response.Status.INTERNAL_SERVER_ERROR.getStatusCode();
	        TgaRestResponse response = new TgaRestResponse(status,"Exception occurred: " + exception.getMessage(), status );
	        return Response.status(status).entity(response).type("application/json").build();
		}

	}
   
}